class Solution
{
public:
    vector<bool> checkIfPrerequisite(int numCourses, vector<vector<int>> &prerequisites, vector<vector<int>> &queries)
    {
        vector<vector<int>> parents(numCourses);
        for (auto &p : prerequisites)
        {
            parents[p[1]].push_back(p[0]);
        }

        vector<unordered_set<int>> ancestors(numCourses);
        for (int i = 0; i < numCourses; ++i)
        {
            queue<int> q;
            q.push(i);
            while (!q.empty())
            {
                int p = q.front();
                q.pop();
                for (int c : parents[p])
                {
                    if (!ancestors[i].count(c))
                    {
                        q.push(c);
                        ancestors[i].insert(c);
                    }
                }
            }
        }

        vector<bool> ans;
        for (auto &q : queries)
        {
            ans.push_back(ancestors[q[1]].count(q[0]));
        }
        return ans;
    }
};